package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.ProductDao;
import com.wanlian.entity.Product;
import com.wanlian.model.ResultModel;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class ProductDaoImpl extends GenericDaoImpl<Product> implements ProductDao {

    public ProductDaoImpl() {
        super(Product.class);
    }

    /**
     * 通过名称查询
     *
     * @param name
     * @return
     */
    @Override
    public Product queryByName(String name) {
        String hql = String.format("from %s where name =?", entityClassName());
        Product product = this.queryForObject(hql, new Object[]{name});
        return product;
    }

    /**
     * 通过名称查询
     *
     * @param name
     * @param page
     * @return
     */
    @Override
    public ResultModel queryByName(String name, Page page) {
        name = String.format("%%s%", name);
        List<Product> lists = new ArrayList<Product>();
        String hql = String.format("from %s where name like ?", entityClassName());
        lists = this.queryForListByPage(hql, new Object[]{name}, page);
        String countsql = String.format("select count(*) from %s where name like ?", entityClassName());
        long count = this.queryCount(countsql, new Object[]{name});
        ResultModel resultModel = new ResultModel();
        resultModel.setObject(lists);
        resultModel.setCount(count);
        return resultModel;
    }

    /**
     * @param pageCondition
     * @return
     */
    @Override
    public ResultModel queryByPageCondition(PageCondition pageCondition) {

        StringBuilder sqlWhere = new StringBuilder("  where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();


        if (pageCondition.isEmpty("id")) {
            int id = Integer.parseInt((String) pageCondition.getByKey("id"));
            sqlWhere.append(" and id=?");
            param.add(id);
        }
        if (pageCondition.isEmpty("name")) {
            String name = (String) pageCondition.getByKey("name");
            sqlWhere.append(" and pname like ?");
            param.add("%"+name+"%");
        }
        if (pageCondition.isEmpty("enable")) {
            int enable = Integer.parseInt((String) pageCondition.getByKey("enable"));
            sqlWhere.append(" and isenable=?");
            param.add(enable);
        }
        if (pageCondition.isEmpty("startTime") && pageCondition.isEmpty("endTime")) {
            String startTime = (String) pageCondition.getByKey("startTime");
            String endTime = (String) pageCondition.getByKey("endTime");
            sqlWhere.append(" and modifytime between date(?) and date(?) ");
            param.add(startTime);
            param.add(endTime);
        }
        //下架或是库存为0，app端不显示
        if (!pageCondition.isEmpty("flag")) {
            sqlWhere.append(" and isenable=true and stock!=0 ");
        }

        sqlWhere.append(" order by id desc ");

        List<Product> lists = new ArrayList<Product>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        lists = this.queryForListByPage(hql, param.toArray(), pageCondition.getPage());
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;

    }
}
